package me.zhengjie.network;


import me.zhengjie.enums.MesRequsetEnum;
import me.zhengjie.vo.Result;
import me.zhengjie.vo.mes.ResponseResult;
import org.springframework.web.client.RestTemplate;


public class BaseNetworkRequest {

    // 创建 RestTemplate 实例
    private final static RestTemplate restTemplate = new RestTemplate();


    public static ResponseResult<?> requestMES(String message, Object request, String requestURL) {
        try {
            ResponseResult<?> responseResult = restTemplate.postForObject(requestURL, request, ResponseResult.class);
            if (responseResult == null || responseResult.getReturnCode() != 200) {
                int count = 0;
                while (!(responseResult == null || responseResult.getReturnCode() == 200) && count < 5) {
                    responseResult = restTemplate.postForObject(requestURL, request, ResponseResult.class);
                    count++;
                }
            }
            if (responseResult != null && responseResult.getReturnCode() == 200) {
                return responseResult;
            } else {
                throw new RuntimeException(message + "  调用MES接口失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException(message + "  调用MES接口失败: " + e.getMessage());
        }
    }


    public static Result<?> requestWCS(String message, Object request, String requestURL) {
        try {
            Result<?> responseResult = restTemplate.postForObject(requestURL, request, Result.class);

            if (responseResult == null || responseResult.getCode() != 200) {
                int count = 0;
                while (!(responseResult == null || responseResult.getCode() == 200) && count < 5) {
                    responseResult = restTemplate.postForObject(requestURL, request, Result.class);
                    count++;
                }
            }
            if (responseResult != null && responseResult.getCode() == 200) {
                return responseResult;
            } else {
                throw new RuntimeException(message + "  调用WCS接口失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException(message + "  调用WCS接口失败: " + e.getMessage());
        }
    }


}
